package com.yidu.manage.controller.base;

import com.yidu.common.core.page.TableDataInfo;
import com.yidu.common.utils.ResultUtil;
import com.yidu.common.utils.StringUtils;
import com.yidu.manage.domain.SysDictData;
import com.yidu.manage.service.ISysDictDataService;
import com.yidu.manage.service.ISysDictTypeService;
import com.yidu.manage.service.system.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author lsm
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @PreAuthorize("@ss.hasPermit('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request, SysDictData dictData)
    {
        startPage(request);
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

//    @PreAuthorize("@ss.hasPermit('system:dict:export')")
//    @GetMapping("/export")
//    public Object export(SysDictData dictData)
//    {
//        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<>(SysDictData.class);
//        return util.exportExcel(list, "字典数据");
//    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermit('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public Object getInfo(@PathVariable Long dictCode)
    {
        return ResultUtil.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public Object dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<>();
        }
        return ResultUtil.success(data);
    }

//    /**
//     * 新增字典类型
//     */
//    @PreAuthorize("@ss.hasPermit('system:dict:add')")
//    @PostMapping
//    public Object add(@Validated @RequestBody SysDictData dict)
//    {
//        // currentUser
//        dict.setCreateBy(SecurityUtils.getUsername());
//        return toAjax(dictDataService.insertDictData(dict));
//    }

//    /**
//     * 修改保存字典类型
//     */
//    @PreAuthorize("@ss.hasPermit('system:dict:edit')")
//    @PutMapping
//    public Object edit(@Validated @RequestBody SysDictData dict)
//    {
//        dict.setUpdateBy(SecurityUtils.getUsername());
//        return toAjax(dictDataService.updateDictData(dict));
//    }

//    /**
//     * 删除字典类型
//     */
//    @PreAuthorize("@ss.hasPermit('system:dict:remove')")
//    @DeleteMapping("/{dictCodes}")
//    public Object remove(@PathVariable Long[] dictCodes)
//    {
//        return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
//    }
}
